<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class DAPP extends NotORM
{

    protected function getTableName($id)
    {
        return 'dapp';
    }

    public function getLunbo()
    {
        $sql = "select dapp.id,p.lunbo_image_url as img,dapp.dapp_name as `name`,dapp.description as `desc`,dapp.category from dapp left join dapp_preview as p on dapp.id = p.dapp_id where dapp.is_lunbo = 1 and dapp.is_mobile != 0";
        $data = \PhalApi\DI()->dapp->dapp->queryAll($sql);
        if(!empty($data))
            return $data;
        return [];
    }

    public function getAppByTop()
    {
        $sql = "select dapp.id,dapp.dapp_name as `name`,dapp.description as `desc`,dapp.website_url as href,p.logo_image_url as icon,p.lunbo_image_url as img,dapp.category from dapp left join dapp_preview as p on dapp.id = p.dapp_id where dapp.status = 1 and is_mobile = 2 order by sort_weight desc limit 3";
        $data = \PhalApi\DI()->dapp->dapp->queryAll($sql);
        if(!empty($data))
            return $data;
        return [];
    }

    public function getApp($category)
    {
        $sql = "select dapp.id,dapp.dapp_name as `name`,dapp.description as `desc`,dapp.website_url as href,p.logo_image_url as icon,p.lunbo_image_url as img from dapp left join dapp_preview as p on dapp.id = p.dapp_id where dapp.status = 1 and  category = $category and dapp.is_mobile != 0 order by sort_weight desc limit 3";
        $data = \PhalApi\DI()->dapp->dapp->queryAll($sql);
        if(!empty($data))
            return $data;
    }

    public function getAppAll($p, $category, $page)
    {
        if($category === 'null')
        {
            $where = '';
        }
        elseif($category === 'popular')
        {
            $where  = "and is_mobile = 2";
        }
        else
        {
            $where  = "and category = $category";
        }

        $sql = "select dapp.id,dapp.dapp_name as `name`,dapp.description as `desc`,dapp.website_url as href,p.logo_image_url as icon,p.lunbo_image_url as img,dapp.category from dapp left join dapp_preview as p on dapp.id = p.dapp_id where  dapp.status = 1  $where and dapp.is_mobile != 0 order by sort_weight desc limit " . ($p - 1) * $page . ", ".$page;
        $data = \PhalApi\DI()->dapp->dapp->queryAll($sql);
        if(!empty($data))
            return $data;
        return [];
    }

}